package com.example.mobile2021606134chhnewdemo.WeatherModule;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;

import com.example.mobile2021606134chhnewdemo.ContactModule.ContactActivity;
import com.example.mobile2021606134chhnewdemo.MapModule.MapActivity;
import com.example.mobile2021606134chhnewdemo.NewsModule.NewsActivity;
import com.example.mobile2021606134chhnewdemo.R;
import com.example.mobile2021606134chhnewdemo.VideoModule.VideoActivity;
import com.example.mobile2021606134chhnewdemo.WeatherModule.fragment.AirFragment;
import com.example.mobile2021606134chhnewdemo.WeatherModule.fragment.FutureFragment;
import com.example.mobile2021606134chhnewdemo.WeatherModule.fragment.PlaceWeatherFragment;
import com.google.android.material.navigation.NavigationView;

import java.util.ArrayList;
import java.util.List;

import me.sugarkawhi.bottomnavigationbar.BottomNavigationBar;
import me.sugarkawhi.bottomnavigationbar.BottomNavigationEntity;

public class WeatherActivity extends AppCompatActivity {
    List<BottomNavigationEntity> entities = new ArrayList<>();
    private BottomNavigationBar bottomNavigationBar;
    private Toolbar toolbar;
    private DrawerLayout drawerlayout;
    private FrameLayout framelayout;
    private NavigationView navigation;
    private BottomNavigationBar BNBWeather;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_weather);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
        initBottomBar();
        initView();
        setSupportActionBar(toolbar);
        getSupportActionBar( ).setDisplayHomeAsUpEnabled(true);
        getSupportActionBar( ).setHomeButtonEnabled(true);
        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerlayout,toolbar,R.string.app_name,R.string.app_name){
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };
        toggle.syncState();
        drawerlayout.addDrawerListener(toggle);
        getSupportFragmentManager().beginTransaction().add(R.id.framelayout, PlaceWeatherFragment.newInstance("",R.drawable.run),"").commit();
        toolbar.setTitle("城市天气");
        navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener( ) {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                item.setCheckable(true);
                switch (item.getItemId()){
                    case R.id.itemweather:
                        getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,PlaceWeatherFragment.newInstance("",R.drawable.run),"").commit();
                        toolbar.setTitle("城市天气");
                        break;
                    case R.id.itemFuture:
                        getSupportFragmentManager().beginTransaction().replace(R.id.framelayout, FutureFragment.newInstance("",""),"").commit();
                        toolbar.setTitle("未来天气");
                        break;
                    case R.id.itemAir:
                        getSupportFragmentManager().beginTransaction().replace(R.id.framelayout, AirFragment.newInstance("",""),"").commit();
                        toolbar.setTitle("空气质量");
                        break;

                    case R.id.itemExit:
                        WeatherActivity.this.finish();
                        break;
                }
                drawerlayout.closeDrawers();
                return true;
            }
        });
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);
        framelayout = (FrameLayout) findViewById(R.id.framelayout);
        navigation = (NavigationView) findViewById(R.id.navigation);
        BNBWeather = (BottomNavigationBar) findViewById(R.id.BNBWeather);
    }

    //
    private void initBottomBar() {
        entities.add(new BottomNavigationEntity("通讯", R.drawable.communication_d, R.drawable.communication_s));
        entities.add(new BottomNavigationEntity("视频", R.drawable.play_d, R.drawable.play_s));
        entities.add(new BottomNavigationEntity("   ", R.drawable.new_d, R.drawable.new_s));
        entities.add(new BottomNavigationEntity("导航", R.drawable.navigation_d, R.drawable.navigation_s));
        entities.add(new BottomNavigationEntity("天气", R.drawable.weather_d, R.drawable.weather_s));
        bottomNavigationBar = findViewById(R.id.BNBWeather);
        bottomNavigationBar.setEntities(entities);
        bottomNavigationBar.setCurrentPosition(4);
        bottomNavigationBar.setBnbItemDoubleClickListener(new BottomNavigationBar.IBnbItemDoubleClickListener() {
            @Override
            public void onBnbItemDoubleClick(int position) {
                Log.i("bottomNavigationBar", "重复选中" + position);
            }
        });
        bottomNavigationBar.setBnbItemSelectListener(new BottomNavigationBar.IBnbItemSelectListener() {
            @Override
            public void onBnbItemSelect(int position) {
                Log.i("bottomNavigationBar", "选中" + position);
                switch (position) {
                    case 0:
                        Intent intent3 = new Intent(getApplicationContext(), ContactActivity.class);
                        startActivity(intent3);
                        finish();
                        overridePendingTransition(0, 0);
                        break;
                    case 1:
                        Intent intent4 = new Intent(getApplicationContext(), VideoActivity.class);
                        startActivity(intent4);
                        finish();
                        overridePendingTransition(0, 0);
                        break;
                    case 2:
                        Intent intent = new Intent(getApplicationContext(), NewsActivity.class);
                        startActivity(intent);
                        finish();
                        overridePendingTransition(0, 0);
                        break;
                    case 3:
                        Intent intent1 = new Intent(getApplicationContext(), MapActivity.class);
                        startActivity(intent1);
                        finish();
                        overridePendingTransition(0, 0);
                        break;
                    case 4:
                        break;
                }
            }
        });
    }
//
//    private void initTitleBar() {
//    }
}